'use strict';
const crypto = require('crypto');

module.exports = app => {
  class SpaceService extends app.Service {
    /**
     * 创建机构，如果发现机构存在，返回错误信息，告知机构已经存在，不能创建
     * 如果机构不存在，那么就进行创建
     */
    * create(data) {
      let query;
      if (data && data.myid) {
        query = { myid: data.myid };
        delete data.myid;
      } else {
        return this.fail('please provide valid data!');
      }

      const created = yield app.model.space.findOrCreate(query, data);
      if (created) {
        if (created.isNew === false) {
          return this.fail('space exist already! noted that myid for space must be unique.');
        }
        return this.success(created.doc);
      }
      return this.fail('created space fail for unknown error!');
    }
  }
  return SpaceService;
};
